package com.csii.excelapp.util;

import lombok.extern.slf4j.Slf4j;

import java.io.*;

/**
 * 文件相关工具类
 */
@Slf4j
public class FileUtils {
    /**
     * 文件重命名，不删除源文件,如果新名字已存在，则忽略
     *
     * @param oldUrl
     * @param newUrl
     */
    public static void reName(String oldUrl, String newUrl) {
        // 旧的文件或目录
        File oldFile = new File(oldUrl);
        // 新的文件或目录
        File newFile = new File(newUrl);
        oldFile.renameTo(newFile);
    }

    public static void copyFileUsingStream(String oldUrl, String newUrl) {
        InputStream is = null;
        OutputStream os = null;
        try {
            is = new FileInputStream(new File(oldUrl));
            os = new FileOutputStream(new File(newUrl));
            byte[] buffer = new byte[1024];
            int length;
            while ((length = is.read(buffer)) > 0) {
                os.write(buffer, 0, length);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(is != null)is.close();
                if(os != null)os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
